package com.anos.smsfun.services;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.telephony.SmsManager;
import android.widget.Toast;

import com.anos.smsfun.R;
import com.anos.smsfun.model.MessageItem;
import com.anos.smsfun.task.QueryUtils;
import com.anos.smsfun.utility.DefConstants;

public class smsSentReceiver extends BroadcastReceiver {

	private MessageItem msgItem;
	private Handler mHandler;

	public smsSentReceiver(MessageItem msg, Handler handler) {
		this.msgItem = msg;
		this.mHandler = handler;
	}

	@Override
	public void onReceive(final Context context, Intent intent) {
		try {
			int what = -1;
			switch (getResultCode()) {
			case Activity.RESULT_OK:
				Toast.makeText(
						context.getApplicationContext(),
						context.getApplicationContext().getString(
								R.string.toast_SMS_sent)
								+ " [" + msgItem.getAddress() + "]",
						Toast.LENGTH_SHORT).show();

				// update UI
				what = DefConstants.WHAT_SENT_RESULT_OK;

				// udpate sms/sent
				msgItem.setStatus(1);
				break;
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
				Toast.makeText(context.getApplicationContext(),
						"Generic failure", Toast.LENGTH_SHORT).show();

				// udpate sms/sent
				msgItem.setStatus(32);
				break;
			case SmsManager.RESULT_ERROR_NO_SERVICE:
				Toast.makeText(
						context.getApplicationContext(),
						context.getApplicationContext().getString(
								R.string.toast_SMS_no_service),
						Toast.LENGTH_SHORT).show();

				// update UI
				what = DefConstants.WHAT_SENT_RESULT_NO_SERVICE;

				// udpate sms/sent
				msgItem.setStatus(32);
				break;
			case SmsManager.RESULT_ERROR_NULL_PDU:
				Toast.makeText(context.getApplicationContext(), "Null PDU",
						Toast.LENGTH_SHORT).show();

				// udpate sms/sent
				msgItem.setStatus(32);
				break;
			case SmsManager.RESULT_ERROR_RADIO_OFF:
				Toast.makeText(context.getApplicationContext(), "Radio off",
						Toast.LENGTH_SHORT).show();

				// udpate sms/sent
				msgItem.setStatus(32);
				break;
			}

			// ///////////////////////////// UPDATE
			QueryUtils.updateSmsSentItem(context.getApplicationContext(),
					msgItem);

			mHandler.sendEmptyMessage(what);

		} catch (Exception e) {
		}
	}

}
